package com.sk.common.level1;

import com.alibaba.fastjson.JSON;
import lombok.Data;


import java.io.InputStream;
import java.io.ObjectInputStream;

/**
 * description：请求参数method类型封装
 *
 * @author 孙文斌
 * @Date: 2021/1/25 18:11
 */
@Data
public class MethodParamRpc {

    String className;
    String methodName;
    Object[] arguments;
    Class<?>[] parameterTypes;

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public static MethodParamRpc convert(InputStream inputStream) {
        try {
            ObjectInputStream input = new ObjectInputStream(inputStream);
            String className = input.readUTF();
            String methodName = input.readUTF();
            Class<?>[] parameterTypes = (Class<?>[])input.readObject();
            Object[] arguments = (Object[])input.readObject();

            MethodParamRpc conventParam = new MethodParamRpc();
            conventParam.setClassName(className);
            conventParam.setMethodName(methodName);
            conventParam.setArguments(arguments);
            conventParam.setParameterTypes(parameterTypes);
            return conventParam;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("解析请求错误:" + e.getMessage());
        }
    }

}
